package cn.iocoder.yudao.module.trade.verify.service;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.api.trade.verify.dto.TradeVerifyRecordSaveDTO;
import cn.iocoder.yudao.module.trade.verify.dal.dataobject.TradeVerifyRecordDO;
import cn.iocoder.yudao.module.trade.verify.dal.mysql.TradeVerifyRecordMapper;
import cn.iocoder.yudao.module.trade.verify.vo.TradeVerifyRecordPageReqVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.api.trade.ErrorCodeConstants.TRADE_VERIFY_RECORD_NOT_EXISTS;

/**
 * 核销记录 Service 实现类
 *
 * @author 山野羡民
 */
@Service
@Validated
public class TradeVerifyRecordServiceImpl implements TradeVerifyRecordService {

    @Resource
    private TradeVerifyRecordMapper verifyRecordMapper;

    @Override
    public void createVerifyRecord(TradeVerifyRecordSaveDTO saveDTO) {
        // 插入
        TradeVerifyRecordDO verifyRecord = BeanUtils.toBean(saveDTO, TradeVerifyRecordDO.class);
        verifyRecordMapper.insert(verifyRecord);
    }

    @Override
    public void deleteVerifyRecord(Long id) {
        // 校验存在
        validateVerifyRecordExists(id);
        // 删除
        verifyRecordMapper.deleteById(id);
    }

    private void validateVerifyRecordExists(Long id) {
        if (verifyRecordMapper.selectById(id) == null) {
            throw exception(TRADE_VERIFY_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public TradeVerifyRecordDO getVerifyRecord(Long id) {
        return verifyRecordMapper.selectById(id);
    }

    @Override
    public PageResult<TradeVerifyRecordDO> getVerifyRecordPage(TradeVerifyRecordPageReqVO pageReqVO) {
        return verifyRecordMapper.selectPage(pageReqVO);
    }

}